import React, { useEffect, useState } from 'react';
import { Button, Checkbox, Input, Radio, Select, notification, Form } from 'antd';
import * as qq from '../../../../api/Gml/agm'
import { useLocation, useNavigate, useSearchParams } from 'react-router-dom';
interface P { }
function Xz(props: P) {

    const [form] = Form.useForm();

    const [value, setValue] = useState('');
    const { state: row } = useLocation()
    var navigate = useNavigate()


    //点击确认按钮
    const onFinish = async (values: any) => {
        // console.log('Success:', values);
        if (!row) {  //添加
            let res = await qq.kfzAdd(values)
            // console.log(res);
            if (res.statusText == 'Created') {
                navigate('/index/attendance/attendance_manage/attendance_group_manage')
                notification.success({ message: '添加成功！' })
            }
        }else {   //修改           
            let res = await qq.kfzUpdate(
                {
                    id:row.id,
                    ...values
                }
            )
            if(res.statusText=='OK'){
                navigate('/index/attendance/attendance_manage/attendance_group_manage')
                notification.success({ message: '修改成功！' })
            }
            
        }
    };


  
//回显表单数据
    useEffect(
        () => {
            console.log(row);
            if (row) {
                form.setFieldsValue(row)
                setValue(row.type)//单选框回显
            } else {
                form.setFieldsValue('')
            }

        }, []
    )


    console.log(value);
    return (
        <div className='kqz'>
            <h2 className='h2'>新增考勤组</h2>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
                style={{ maxWidth: 600 }}
                initialValues={{ remember: true }}
                onFinish={onFinish}
                autoComplete="off"
            >
                <Form.Item
                    label="考勤组名称"
                    name="name"
                    rules={[{ required: true, message: 'Please input your name!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="项目成员"
                    name="num"
                    rules={[{ required: true, message: 'Please input your num' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="班次"
                    name="type"
                    rules={[{ required: true, message: 'Please input your type' }]}
                >
                    <Radio.Group name="radiogroup" value={value}>
                        <Radio value={'固定班制'}>固定班制</Radio>
                        <Radio value={'排班制'}>排班制</Radio>
                        <Radio value={'自由班制'}>自由班制</Radio>
                    </Radio.Group>
                </Form.Item>

                <Form.Item
                    label="考勤时间"
                    name="time"
                    rules={[{ required: true, message: 'Please input your time' }]}
                >
                    <Select
                        defaultValue="默认8:00-18:00"
                        style={{ width: 120 }}
                        options={[
                            { value: '默认8:00-18:00', label: '默认8:00-18:00' },
                            { value: '早班:7:00-16:00', label: '早班:7:00-16:00' },
                            { value: '晚班:20:00-02:00', label: '晚班:20:00-00:00' },
                        ]}
                    />
                </Form.Item>

                <Form.Item
                    label="考勤组负责人"
                    name="people"
                    rules={[{ required: true, message: 'Please input your num' }]}
                >
                    <Select
                        defaultValue=""
                        style={{ width: 120 }}
                        options={[
                            { value: '辛', label: '辛' },
                            { value: '远', label: '远' },
                            { value: '东', label: '东' },
                            { value: '远东', label: '远东' },
                            { value: '辛东', label: '辛东' },
                        ]}
                    />
                </Form.Item>


                <Form.Item wrapperCol={{ offset: 8, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        添加
                    </Button>
                </Form.Item>
            </Form>
        </div>
    )
}

export default Xz;